package com.ruoyi.ldzlsb.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ldzlsb.mapper.SbInspectionCycleConfigMapper;
import com.ruoyi.ldzlsb.domain.SbInspectionCycleConfig;
import com.ruoyi.ldzlsb.service.ISbInspectionCycleConfigService;

/**
 * 巡检周期配置Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SbInspectionCycleConfigServiceImpl implements ISbInspectionCycleConfigService 
{
    @Autowired
    private SbInspectionCycleConfigMapper sbInspectionCycleConfigMapper;

    /**
     * 查询巡检周期配置
     * 
     * @param configId 巡检周期配置ID
     * @return 巡检周期配置
     */
    @Override
    public SbInspectionCycleConfig selectSbInspectionCycleConfigById(Long configId)
    {
        return sbInspectionCycleConfigMapper.selectSbInspectionCycleConfigById(configId);
    }

    /**
     * 查询巡检周期配置列表
     * 
     * @param sbInspectionCycleConfig 巡检周期配置
     * @return 巡检周期配置
     */
    @Override
    public List<SbInspectionCycleConfig> selectSbInspectionCycleConfigList(SbInspectionCycleConfig sbInspectionCycleConfig)
    {
        return sbInspectionCycleConfigMapper.selectSbInspectionCycleConfigList(sbInspectionCycleConfig);
    }
    
    /**
     * 根据计划ID查询巡检周期配置
     * 
     * @param planId 巡检计划ID
     * @return 巡检周期配置
     */
    @Override
    public SbInspectionCycleConfig selectSbInspectionCycleConfigByPlanId(Long planId)
    {
        return sbInspectionCycleConfigMapper.selectSbInspectionCycleConfigByPlanId(planId);
    }

    /**
     * 新增巡检周期配置
     * 
     * @param sbInspectionCycleConfig 巡检周期配置
     * @return 结果
     */
    @Override
    public int insertSbInspectionCycleConfig(SbInspectionCycleConfig sbInspectionCycleConfig)
    {
        sbInspectionCycleConfig.setCreateTime(DateUtils.getNowDate());
        sbInspectionCycleConfig.setCreateBy(SecurityUtils.getUsername());
        return sbInspectionCycleConfigMapper.insertSbInspectionCycleConfig(sbInspectionCycleConfig);
    }

    /**
     * 修改巡检周期配置
     * 
     * @param sbInspectionCycleConfig 巡检周期配置
     * @return 结果
     */
    @Override
    public int updateSbInspectionCycleConfig(SbInspectionCycleConfig sbInspectionCycleConfig)
    {
        sbInspectionCycleConfig.setUpdateTime(DateUtils.getNowDate());
        sbInspectionCycleConfig.setUpdateBy(SecurityUtils.getUsername());
        return sbInspectionCycleConfigMapper.updateSbInspectionCycleConfig(sbInspectionCycleConfig);
    }
    
    /**
     * 保存或更新周期配置
     * 
     * @param sbInspectionCycleConfig 巡检周期配置
     * @return 结果
     */
    @Override
    public int saveOrUpdateCycleConfig(SbInspectionCycleConfig sbInspectionCycleConfig)
    {
        // 检查是否已存在
        SbInspectionCycleConfig existConfig = sbInspectionCycleConfigMapper.selectSbInspectionCycleConfigByPlanId(sbInspectionCycleConfig.getPlanId());
        
        if (existConfig != null) {
            // 更新现有配置
            existConfig.setCycleType(sbInspectionCycleConfig.getCycleType());
            existConfig.setCycleValue(sbInspectionCycleConfig.getCycleValue());
            existConfig.setConfigData(sbInspectionCycleConfig.getConfigData());
            return updateSbInspectionCycleConfig(existConfig);
        } else {
            // 新增配置
            return insertSbInspectionCycleConfig(sbInspectionCycleConfig);
        }
    }

    /**
     * 批量删除巡检周期配置
     * 
     * @param configIds 需要删除的巡检周期配置ID
     * @return 结果
     */
    @Override
    public int deleteSbInspectionCycleConfigByIds(Long[] configIds)
    {
        return sbInspectionCycleConfigMapper.deleteSbInspectionCycleConfigByIds(configIds);
    }

    /**
     * 删除巡检周期配置信息
     * 
     * @param configId 巡检周期配置ID
     * @return 结果
     */
    @Override
    public int deleteSbInspectionCycleConfigById(Long configId)
    {
        return sbInspectionCycleConfigMapper.deleteSbInspectionCycleConfigById(configId);
    }
    
    /**
     * 根据计划ID删除周期配置
     * 
     * @param planId 巡检计划ID
     * @return 结果
     */
    @Override
    public int deleteSbInspectionCycleConfigByPlanId(Long planId)
    {
        return sbInspectionCycleConfigMapper.deleteSbInspectionCycleConfigByPlanId(planId);
    }
    
    /**
     * 批量删除巡检周期配置通过计划ID
     * 
     * @param planIds 计划ID数组
     * @return 结果
     */
    @Override
    public int deleteSbInspectionCycleConfigByPlanIds(Long[] planIds)
    {
        return sbInspectionCycleConfigMapper.deleteSbInspectionCycleConfigByPlanIds(planIds);
    }
} 